
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
      --><title>modchimodDtFacpInfExp</title><meta name="generator" content="MATLAB 7.11"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2011-11-04"><meta name="DC.source" content="modchimodDtFacpInfExp.m"><style type="text/css">

body {
  background-color: white;
  margin:10px;
}

h1 {
  color: #990000; 
  font-size: x-large;
}

h2 {
  color: #990000;
  font-size: medium;
}

/* Make the text shrink to fit narrow windows, but not stretch too far in 
wide windows. */ 
p,h1,h2,div.content div {
  max-width: 600px;
  /* Hack for IE6 */
  width: auto !important; width: 600px;
}

pre.codeinput {
  background: #EEEEEE;
  padding: 10px;
}
@media print {
  pre.codeinput {word-wrap:break-word; width:100%;}
} 

span.keyword {color: #0000FF}
span.comment {color: #228B22}
span.string {color: #A020F0}
span.untermstring {color: #B20000}
span.syscmd {color: #B28C00}

pre.codeoutput {
  color: #666666;
  padding: 10px;
}

pre.error {
  color: red;
}

p.footer {
  text-align: right;
  font-size: xx-small;
  font-weight: lighter;
  font-style: italic;
  color: gray;
}

  </style></head><body><div class="content"><h2>Contents</h2><div><ul><li><a href="#3"><b>I. Description for CHIMOD DT FACP X SPREAD NEWS4</b></a></li><li><a href="#4"><b>II. Input</b></a></li><li><a href="#5"><b>III. Output</b></a></li><li><a href="#6"><b>IV Notes on Multiple Prices</b></a></li><li><a href="#7"><b>V. Notes on Parametrization</b></a></li><li><a href="#8"><b>IV.a Model and data means</b></a></li><li><a href="#9"><b>V. Related models</b></a></li><li><a href="#10"><b>Version</b></a></li><li><a href="#11"><b>PART 1. Declare All Variables in the Model Solution <img src="modchimodDtFacpInfExp_eq30760.png" alt="$$ S_{t} $$"></b></a></li><li><a href="#12">Expectations of inflation 1 through 40 quarters ahead</a></li><li><a href="#13"><b>PART 2. Declare Innovations to the exogenous variables <img src="modchimodDtFacpInfExp_eq07038.png" alt="$$ \eta_{t} $$"></b></a></li><li><a href="#14"><b>PART 3. Declare Parameters</b></a></li><li><a href="#15">Parameters for inflation expectations</a></li><li><a href="#16"><b>PART 4. Compute Steady State (SS)</b></a></li><li><a href="#17"><b>PART 5. Declare System Matrices</b></a></li><li><a href="#18"><b>PART 6. Begin Filling Equation Rows</b></a></li><li><a href="#19">Eq 1, Output (y) from production function</a></li><li><a href="#20">Eq. 2, Effective Capital (K)</a></li><li><a href="#21">Eq. 3 Hours (L), from cost minimization</a></li><li><a href="#22">Eq. 4 Return to Capital (Rk) from utilization FOC</a></li><li><a href="#23">Eq. 5, Real Wages (w) PC (with split of shocks)</a></li><li><a href="#24">Eq. 6, Wage Gap (x), (MRS-Real Wage)</a></li><li><a href="#25">Eq 7, Prices (p) PC</a></li><li><a href="#26">Eq 8, Marginal Cost (s)</a></li><li><a href="#27">Eq 9, Lagrange Multiplier IBC (Lambda)</a></li><li><a href="#28">Eq 10, Consumption (c)</a></li><li><a href="#29">Eq 11, Taylor Rule for Nominal Interest Rate (R)</a></li><li><a href="#30">Eq. 12, Utilization (U) from market clearing condition</a></li><li><a href="#31">Eq. 13, Shadow Value of Capital (phi)</a></li><li><a href="#32">Eq. 14, Investment (i, in consumption units)</a></li><li><a href="#33">Eq 15, Capital accumulation (kbar)</a></li><li><a href="#34">Eq 16, MP Exogenous Policy Innovation (unexpected)</a></li><li><a href="#35">Eq 17, Exogenous Growth Rate in Neutral Technoloy</a></li><li><a href="#36">Eq 18, Exogenous Process in G Spending</a></li><li><a href="#37">Eq 19, Exogenous Process in Investment Specific Tech or Relative Price C</a></li><li><a href="#38">Eq 20, Exogenous Process in Price Markup</a></li><li><a href="#39">Eq 21, Exogenous Process in Labor Disutility (AR1)</a></li><li><a href="#40">Eq 22, Exogenous Process in Intertemporal Preference</a></li><li><a href="#41">Eq 23, Exogenous Process in MEI</a></li><li><a href="#42">Eq 24, Exogenous Process in Inflation Drift</a></li><li><a href="#43">Eq 25, Exogenous Process in Wage Markup (white noise)</a></li><li><a href="#44">Eq. 26, Observable GDP Nominal (add P back)</a></li><li><a href="#45">Eq. 27, Observable Consumption Nominal (add P back)</a></li><li><a href="#46">Eq. 28 Observable Investment Nominal (add P back)</a></li><li><a href="#47">Eq. 29 Observable Hourly Wages  Nominal (add P back)</a></li><li><a href="#48">Eq. 30 Observable Real GDP Growth (in C Units)</a></li><li><a href="#49">Eq. 31 GDP (detrended), Real Detrended Output corrected for Utilization</a></li><li><a href="#50">Eq. 32 Growth Rate in Investment Deflator</a></li><li><a href="#51">Eq. 33 GDP Deflator</a></li><li><a href="#52">Eq. 34 Lag 1 Observable Real GDP Growth (in C Units)</a></li><li><a href="#53">Eq. 35 Lag 2 Observable Real GDP Growth (in C Units)</a></li><li><a href="#54">Eq. 36 Annualized Observable Real GDP Growth (in C Units)</a></li><li><a href="#55">Eq. 37 Lag 1 Inflation (C)</a></li><li><a href="#56">Eq. 38 Lag 2 Inflation (C)</a></li><li><a href="#57">Eq. 39 Annualized Inflation (C)</a></li><li><a href="#58">Eq. 40, Growth Rate in Aggregate Trend</a></li><li><a href="#59">Eq. 45-49 Monetary Policy Signals</a></li><li><a href="#60">Eq. 45. <img src="modchimodDtFacpInfExp_eq34073.png" alt="$$ \zeta^{4}_{t}       = \epsilon^{4}_{t} $$"></a></li><li><a href="#61">Eq. 46.  <img src="modchimodDtFacpInfExp_eq08731.png" alt="$$ \zeta^{3}_{t} = \zeta^{4}_{t-1} + \epsilon^{3}_{t} $$"></a></li><li><a href="#62">Eq. 47.  <img src="modchimodDtFacpInfExp_eq75028.png" alt="$$ \zeta^{2}_{t} = \zeta^{3}_{t-1} + \epsilon^{2}_{t} $$"></a></li><li><a href="#63">Eq. 48.  <img src="modchimodDtFacpInfExp_eq04683.png" alt="$$ \zeta^{1}_{t} = \zeta^{2}_{t-1} + \epsilon^{1}_{t} $$"></a></li><li><a href="#64">Eq. 49. <img src="modchimodDtFacpInfExp_eq01231.png" alt="$$ \zeta^{0}_{t} = \zeta^{1}_{t-1} $$">, which implies that the history of</a></li><li><a href="#66"><b>PART 7: Solve Model using AMASOLVE</b></a></li><li><a href="#67"><b>PART 8: Addition of Measurement Equations for Prices</b></a></li><li><a href="#68">8.1 Extend model solution by 2*NSERP rows</a></li><li><a href="#69">8.2 Idiosyncratic errors</a></li><li><a href="#70">8.3 General Structure of Observable Price Equation</a></li><li><a href="#71">8.3.a First row is GDP</a></li><li><a href="#72">8.3.b Remaining rows are multiple indicators for consumption prices</a></li><li><a href="#73">8.3.c Add idiosyncratic errors</a></li><li><a href="#74"><b>Part 9: Addition of Measurement Equations for Spread</b></a></li><li><a href="#75">9.1 Extend model solution by 1 row</a></li><li><a href="#76">9.2 Fill in last row for the spread</a></li><li><a href="#77"><b>PART 10: Declare Cholesky of Variance Covariance Matrix</b></a></li><li><a href="#78"><b>PART 11: Declare ZZ, Matrix of Pointers to Observables</b></a></li><li><a href="#79"><b>PART 12: Declare C, Matrix of Constants</b></a></li><li><a href="#80">16.1 STANAMES: Cell with Names of ALL states</a></li><li><a href="#81">16.2 STSHOCKS: Cell with Names of Shocks in STANAMES</a></li><li><a href="#82">16.3 SHONAMES: Cell with Names of Shock Innovations</a></li><li><a href="#83">16.4 Quick check dimensions</a></li></ul></div><pre class="codeinput"> <span class="keyword">function</span> [GG,impact,C,eu,SDX,ZZ,initss,ssvec,flag,ssnames,stanames,shonames]=modchimodDtFacpInfExp(parest,options,op,calval,posest,poscal)
</pre><pre class="codeinput"><span class="comment">% =========================================================================</span>
<span class="comment">%</span>
<span class="comment">% Last Modified July 25 2011. Compatibility with code ion</span>
<span class="comment">%</span>
<span class="comment">% function [GG,C,impact,eu,SDX,ZZ,NY,NX,NETA,gev,ncof]=...</span>
<span class="comment">% modchimodDtFacpXSpreadNews(parest,calval,posest,poscal)</span>
<span class="comment">%</span>
</pre><h2><b>I. Description for CHIMOD DT FACP X SPREAD NEWS4</b><a name="3"></a></h2><p>Model code for Chicago Fed model (CHIMOD) with Double Trend (DT) Factor structure for prices (FACP) Separates the ARMA(1,1) wage markup/labor disutility shock into a white noise wage markup and an AR(1) labor disutility shock (X) with a spread observable (SPREAD) and allowing for policy news shocks upto 4 periods ahead (NEWS4).</p><h2><b>II. Input</b><a name="4"></a></h2><p>There are either 1 or 4 inputs <b>CASE 1</b> If there is 1 input, then PAREST should be the whole parameter vector.</p><p><b>CASE 2</b> If there are 4 inputs, the parameter vector PARAM is created from the estimated and calibrated values. PAREST are the estimated values in rows POSEST and CALVAL are the calibrated values in position POSCAL.</p><h2><b>III. Output</b><a name="5"></a></h2><p>The model to be solved is written, inside this code, as <img src="modchimodDtFacpInfExp_eq18474.png" alt="$$ A_{0}Y_{t+1} + A_{1}Y_{t}=A_{2}Y_{t-1} + PSI \eta_{t} $$">  where the A matrices are filled with the linearized equation and entries depend on the parameter vector PARAM.</p><p>The solution of the model is a state space system characterized by</p><p><b>Transition Equation</b> <img src="modchimodDtFacpInfExp_eq82566.png" alt="$$ S_{t} = GG S_{t-1} + RR \eta_{t} $$"> and variance covariance matrix, where <img src="modchimodDtFacpInfExp_eq30760.png" alt="$$ S_{t} $$"> is the vector of all variables (endogenous and exogenous) of the model of dimension [NY 1], and <img src="modchimodDtFacpInfExp_eq25830.png" alt="$$ \eta $$">  is the vector of exogenous innovations of dimension [NX 1] with variance covariance matrix <img src="modchimodDtFacpInfExp_eq33093.png" alt="$$ V(\eta) = SDX'SDX $$">. Matrix dimensions: GG [NY NY], IMPACT [NY NX], SDX [NX NX].</p><p><b>Measurement Equation</b> <img src="modchimodDtFacpInfExp_eq26502.png" alt="$$ Y_{t} = ZZ S_{t} + ZZ C  $$"> , where <img src="modchimodDtFacpInfExp_eq93168.png" alt="$$ Y_{t} $$"> is the data vector of dimension [NZ 1], <img src="modchimodDtFacpInfExp_eq67385.png" alt="$$ ZZ $$"> is a selection matrix indicating which states are observed, and <img src="modchimodDtFacpInfExp_eq46991.png" alt="$$ CC $$"> is a vector of constants of dimension [NY 1].</p><p>EU is a [2 1] vector equal to [1;1] if there is a unique stable RE solution</p><p>NETA GEV and COF are obsolete outputs that are still called in some codes and will be deleted in the new streamlined version of the estimation routine.</p><h2><b>IV Notes on Multiple Prices</b><a name="6"></a></h2><p>This version accomodates as observables an Investment Deflator and an additional NSERP prices. Of these NSERP prices the first one should be the GDP deflator and the remaining NSERP-1 series multiple observables for the Consumption Deflator (e.g. chain weighted model consistent, Core PCE, Core CPI).</p><h2><b>V. Notes on Parametrization</b><a name="7"></a></h2><p>1. Taylor rule written in terms of annualized output and inflation growth, with an inflation drift. 2. Wage markup and labor disutility shock split. 3. The composite steady state (SS) growth rate of the trend and the (SS) growth rate of ISTS progress are controlled by separate parameterss <b>gamma100</b> and <b>gammamiu100</b> rather than two parameters connected with ALPHA. This facilitates putting priors. 4. AR coefficients of idiosyncratic errors are given by 2*PAR-1, where PAR is the paramater plugged into solution. Hence while PAR should be in the [0,1] interval, the AR coefficients are allowed to go negative.</p><h2><b>IV.a Model and data means</b><a name="8"></a></h2><p>The model based means in the observation equation are given by the parameters <b>gammamiu100</b> and <b>pss100</b> which correspond to the mean growth rate of the relative price of investment to consumption and the mean growth rate of the consumption deflator. In addition, we allow the means of the observed price series to differ from these model based means according to extra parameters, intended to capture the discrepancy between model and data.</p><p>Hence the model based mean of the Investment deflator is obtained as <b>pss100-gammamiu100-CMIU</b>. Similarly, the constants for the Consumption prices  is given by <b>pss100+CP</b> where CP differs by price. For simplicity this is also the constant for the GDP deflator, rather than aggregating that for investment and consumption; historically for instance Core PCE and the GDP deflator have grown at very similar rates.</p><h2><b>V. Related models</b><a name="9"></a></h2><p><b>MOD CHIMOD DT FAC P3 X SPREAD NEWS4 _ OBS:</b> is used to declare additional variables not used in estimation but annalized (e.g. output gap, real rate).</p><p><b>MOD CHIMOD DT FAC P3 SPREAD NEWS4:</b> very similar code but without split of wage markup and labor distulity shocks</p><h2><b>Version</b><a name="10"></a></h2><p>Created by Alejandro Justiniano 12/13/2010</p><p>Last Modified 12/13/2010. Run code ZB_CHECKWAGESPC to ensure alternative form of the wage Phillips curve matches model without split of wage markup and labor disutility shock. Also IRF of I to neutral shock is reasonable. =========================================================================</p><pre class="codeinput"><span class="keyword">if</span> nargout &lt; 11
    flag_repstanames=0;
<span class="keyword">else</span>
    flag_repstanames=1;
<span class="keyword">end</span>
<span class="keyword">if</span> nargout &lt; 10
    flag_repssnames=0;
<span class="keyword">else</span>
    flag_repssnames=1;
<span class="keyword">end</span>

flag.ssok=1;
flag.solver=1;

initss=[];
ssnames=[];
ssvec  =[];


<span class="comment">% if nargin~=1 &amp;&amp; nargin~=4</span>
<span class="comment">%     error('Need either 1 or 4 inputs')</span>
<span class="comment">% end</span>

<span class="comment">% =========================================================================</span>
</pre><h2><b>PART 1. Declare All Variables in the Model Solution <img src="modchimodDtFacpInfExp_eq30760.png" alt="$$ S_{t} $$"></b><a name="11"></a></h2><pre class="codeinput"><span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 1.a Endogenous Variables</span>
y=1;            <span class="comment">% output</span>
k=2;            <span class="comment">% capital</span>
L=3;            <span class="comment">% hours</span>
Rk=4;           <span class="comment">% return on capital</span>
w=5;            <span class="comment">% real wages</span>
x=6;            <span class="comment">% marginal utility of labor</span>
p=7;            <span class="comment">% inflation</span>
s=8;            <span class="comment">% marginal cost</span>
lambda=9;       <span class="comment">% multiplier</span>
c=10;           <span class="comment">% consumption</span>
R=11;           <span class="comment">% interst rate</span>
u=12;           <span class="comment">% capital utilization</span>
phi=13;         <span class="comment">% multiplier</span>
i=14;           <span class="comment">% investment</span>
kbar=15;        <span class="comment">% "gross" capital</span>
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 1.b Exogenous driving processes</span>
mp=16;
z =17;          <span class="comment">% Productivity shock</span>
g =18;          <span class="comment">% Public spending shock</span>
miu=19;         <span class="comment">% Relative price (non stationary), this is Growth Rate</span>
lambdap=20;     <span class="comment">% Markup shock AR(1)</span>
psi=21;         <span class="comment">% Preference shock AR(1)</span>
b=22;           <span class="comment">% Discount factor shock</span>
upsilon=23;     <span class="comment">% MEI, stationary, investment shock</span>
pitarg=24;      <span class="comment">% Inflation Drift</span>
wmarkwn=25;     <span class="comment">% White Noise Wage Markup</span>
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 1.c Observable growth rates (nominal)</span>
ynomobs=26;
cnomobs=27;
inomobs=28;
wnomobs=29;
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 1.c.a Auxiliary Variables or Observable Prices</span>
yrobs   =30;
gdp     =31;
dpinv   =32;
gdpdef  =33;
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 1.d Lags</span>
yrobs_1=34;   <span class="comment">% Real Observable Output (adjusted for utilization) lag1</span>
yrobs_2=35;   <span class="comment">% Real Observable Output (adjusted for utilization) lag2</span>
yobsan =36;   <span class="comment">% Annualize Observable Output</span>
p_1    =37;
p_2    =38;
dpma   =39;   <span class="comment">% Four quarter inflation</span>
dagtrend=40;  <span class="comment">% First difference of the aggregate trend</span>
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 1.e 1 through 4 step ahead expectations of FFR</span>
startstar=40;
<span class="comment">% 1.f Policy signals</span>
stsig=startstar;
sigR_0=stsig+1;
sigR_1=stsig+2;
sigR_2=stsig+3;
sigR_3=stsig+4;
sigR_4=stsig+5;
<span class="comment">% 1.g Inflation Expectations</span>
posExpInf=stsig+5+1;
</pre><h2>Expectations of inflation 1 through 40 quarters ahead<a name="12"></a></h2><pre class="codeinput">pe_vec=(posExpInf):(posExpInf+39);
expectedPi40=posExpInf+40;
NY=posExpInf+40;

<span class="comment">% =========================================================================</span>

<span class="comment">% =========================================================================</span>
</pre><h2><b>PART 2. Declare Innovations to the exogenous variables <img src="modchimodDtFacpInfExp_eq07038.png" alt="$$ \eta_{t} $$"></b><a name="13"></a></h2><pre class="codeinput">nsignals = 4;    <span class="comment">% Number of policy signals</span>
NX=10+ nsignals;
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 2.a Structural Disturbances</span>
Rs=1;           <span class="comment">% MP</span>
zs=2;           <span class="comment">% Technology</span>
gs=3;           <span class="comment">% Public spending</span>
mius=4;         <span class="comment">% Investment</span>
lambdaps=5;     <span class="comment">% Mark-up</span>
psis=6;         <span class="comment">% Leisure preference</span>
bs=7;           <span class="comment">% Discount factor</span>
upsilons=8;     <span class="comment">% investment shock</span>
shpitarg=9;     <span class="comment">% Inflation Drify</span>
wmarkwnsh=10;
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 2.b Policy Signals</span>
Rs_lag1=11;    <span class="comment">% 1 period ahead</span>
Rs_lag2=12;    <span class="comment">% 2 periods ahead</span>
Rs_lag3=13;    <span class="comment">% 3 periods ahead</span>
Rs_lag4=14;    <span class="comment">% 4 periods ahead</span>
<span class="comment">% =========================================================================</span>

<span class="comment">% =========================================================================</span>
</pre><h2><b>PART 3. Declare Parameters</b><a name="14"></a></h2><pre class="codeinput">ncof=        68;    <span class="comment">% Number of coefficients</span>
numpar=      ncof;  <span class="comment">% Equal to Parameters, distinction irrelevant here</span>
<span class="keyword">if</span> nargin &lt; 4 || isempty( calval )
    param=parest  ;
<span class="keyword">else</span>
    param=zeros(numpar,1);
    param(posest)=parest;
    param(poscal)=calval;
<span class="keyword">end</span>;
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.1 Parameters for all but multiple prices, GDP and I deflators</span>

alpha=param(1);         <span class="comment">% share of capital in the prod. function</span>
delta=param(2);         <span class="comment">% capital depreciation rate</span>
iotap=param(3);         <span class="comment">% price indexation (=0 is static indexation, =1 is dynamic)</span>
iotaw=param(4);         <span class="comment">% wages indexation</span>
gammastar100=param(5);  <span class="comment">% steady state growth rate of technology</span>
gammamiu100=param(6);   <span class="comment">% steady state growth rate of capital embodied technology</span>
h=param(7);             <span class="comment">% habit formation parameter</span>
lambdapss=param(8);     <span class="comment">% steady state of mark-up shock (pins down steady state of wages)</span>
lambdaw=param(9);       <span class="comment">% wage mark-up</span>
Lss=param(10);          <span class="comment">% steady state for leisure (the ss for leisure is pinned down by psiss. convenient to parameterize the model in terms of Lss)</span>
pss100=param(11);       <span class="comment">% steady state quarterly inflation (multiplied by 100)</span>
Fbeta=param(12);        <span class="comment">% weird parameter of SW that is a function of beta and the steady state quarterly real rate of interest (ensures that beta is less than 1)</span>
gss=param(13);          <span class="comment">% steady state of the public spending shock</span>
niu=param(14);          <span class="comment">% curvature of the utility function for leisure</span>
xip=param(15);          <span class="comment">% price stickiness</span>
xiw=param(16);          <span class="comment">% wage stickiness</span>
chi=param(17);          <span class="comment">% elasticity of the capital utilization cost function</span>
S=param(18);            <span class="comment">% investment adjustment cost</span>
fp=param(19);           <span class="comment">% reaction to annualized inflation</span>
fy=param(20);           <span class="comment">% reaction to annualized output growth</span>
rhoR=param(21);         <span class="comment">% policy smoothing</span>
<span class="comment">% AR coefficients of exogenous prorcesses</span>
rhoz=param(22);
rhog=param(23);
rhomiu=param(24);
rholambdap=param(25);
rhopsi=param(26);
rhob=param(27);
rhopitarg=param(28);
rhomp=param(29);
rhoupsilon=param(30);
<span class="comment">% Std of structural shocks</span>
sdstruct=param(31:40);
kappa       = -param(41);   <span class="comment">% Scaling for spredd</span>
sdspreadme  =  param(42);   <span class="comment">% Measurement error spread</span>
<span class="comment">% Std Policy Signals</span>
stdsignals  =param(43:46);
<span class="keyword">if</span> length(stdsignals)~=nsignals
    error(<span class="string">'Wrong STD for signals'</span>)
<span class="keyword">end</span>
</pre><pre class="codeoutput">Input argument "parest" is undefined.

Error in ==&gt; modchimodDtFacpInfExp at 237
    param=parest  ; 
</pre><h2>Parameters for inflation expectations<a name="15"></a></h2><pre class="codeinput">posCPI=param(47);
expInfLoading=param(48);
end_nonp=48;
<span class="comment">% _________________________________________________________________________</span>
<span class="comment">% 3.2  Begin Parameters for Multiple Prices</span>
<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.2.1  How to aggregate gdp deflator</span>
flag_wadd1  =param(end_nonp+1);
nserp       =param(end_nonp+2);
<span class="comment">% __________________________________________________________________</span>
<span class="comment">% 3.2.1 Declare positions for</span>
<span class="comment">% Rows for</span>
<span class="comment">% a) loadings</span>
<span class="comment">% b) ar ID errors</span>
<span class="comment">% c) constants</span>
<span class="comment">% d) volatilities</span>
<span class="comment">%</span>
<span class="comment">% *Note:* There are NSERP+1 loadings. First 2 are for the GDP Deflator.</span>
<span class="comment">% 1st of W(1)*Price consumption.2nd on W(2)*Prince Investment.</span>
<span class="comment">% Remaining are for other prices on the Consumption Deflators</span>
row_load=(end_nonp+3):(end_nonp+nserp+3);
row_ar  =row_load(end)*ones(1,nserp)+(1:nserp);
row_std =row_ar(end)*ones(1,nserp)+(1:nserp);
row_c   =row_std(end)*ones(1,nserp)+(1:nserp);

<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.2.3 vector of factor loadings</span>
bet_gdpdef  =param(row_load(1:2));
bet_c       =param(row_load(3:end));

bet_CPI     =bet_c(posCPI-1);
<span class="keyword">if</span> abs(expInfLoading-999) &lt; 0.01;
    expInfLoading=bet_CPI;
<span class="keyword">end</span>



<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.2.4 vector of idiosyncratic errors</span>
rhoid=2*param(row_ar)-1;
<span class="keyword">if</span> any(rhoid &lt; -0.995)~=0 || any(rhoid &gt; 0.995)~= 0
    disp(<span class="string">'Explosive AR ID roots'</span>);
    GG=[];C=[];impact=[];eu=[0;0];SDX=[];ZZ=[];NY=[];NX=[];NETA=[];gev=[];ncof=[];
    <span class="keyword">return</span>
<span class="keyword">end</span>

<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.2.5 vector of constants</span>
cprices =param(row_c);
<span class="keyword">if</span> all( cprices ~= 0 )
    error(<span class="string">'At least one of the constants must be set equal to 0'</span>)
<span class="keyword">end</span>
constantCPI=cprices(posCPI);

<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.2.6 vector of idiosyncratic errors</span>
sdid             =param(row_std);

<span class="comment">%__________________________________________________________________________</span>
<span class="comment">% 3.2.7 constants for I deflator</span>
cons_idef        =param(row_c(end)+1:end);
<span class="keyword">if</span> length(cons_idef)&gt; 1
    error(<span class="string">'Wrong Dimension of Investment Deflator'</span>)
<span class="keyword">end</span>

<span class="comment">%==========================================================================</span>
</pre><h2><b>PART 4. Compute Steady State (SS)</b><a name="16"></a></h2><pre class="codeinput">gammastar=gammastar100/100;
gammamiu=gammamiu100/100;
beta=100/(Fbeta+100);
rss=exp(gammastar)/beta-1;
rss100=rss*100;
gss=1/(1-gss);
Rkss=exp(gammastar+gammamiu)/beta-1+delta;
sss=1/(1+lambdapss);
wss=(sss*((1-alpha)^(1-alpha))/((alpha^(-alpha))*Rkss^alpha))^(1/(1-alpha));
kLss=(wss/Rkss)*alpha/(1-alpha);
FLss=(kLss^alpha-Rkss*kLss-wss);
yLss=kLss^alpha-FLss;

<span class="comment">% Scale of the economy is fixed at 1, i.e. independent of LSS which now</span>
<span class="comment">% only enters observation equation for hours</span>
Lsscale=1;
kss=kLss*Lsscale;
iss=(1-(1-delta)*exp(-gammastar-gammamiu))*kss*exp(gammastar+gammamiu);
F=FLss*Lsscale;
yss=yLss*Lsscale;
css=yss/gss-iss;

ssvec  =[css/yss;iss/yss;kss/yss;rss100;100*Rkss];
<span class="keyword">if</span>  flag_repssnames==1;
    ssnames={<span class="string">'c/y'</span>  ;<span class="string">'i/y'</span>  ;<span class="string">'k/y'</span>;  <span class="string">'rss100'</span>;<span class="string">'Rkss100'</span>};
<span class="keyword">end</span>

<span class="comment">%%==========================================================================</span>

<span class="comment">%==========================================================================</span>
</pre><h2><b>PART 5. Declare System Matrices</b><a name="17"></a></h2><p>Model to be solved is written as <img src="modchimodDtFacpInfExp_eq18474.png" alt="$$ A_{0}Y_{t+1} + A_{1}Y_{t}=A_{2}Y_{t-1} + PSI \eta_{t} $$"></p><pre class="codeinput">A0  = zeros(NY,NY) ;
A1  = zeros(NY,NY) ;
A2  = zeros(NY,NY) ;
PSI = zeros(NY,NX) ;
<span class="comment">%==========================================================================</span>
expg=exp(gammastar);
</pre><h2><b>PART 6. Begin Filling Equation Rows</b><a name="18"></a></h2><h2>Eq 1, Output (y) from production function<a name="19"></a></h2><pre class="codeinput">A1(y,y)=1;
A1(y,k)=-((yss+F)/yss)*alpha;
A1(y,L)=-((yss+F)/yss)*(1-alpha);
</pre><h2>Eq. 2, Effective Capital (K)<a name="20"></a></h2><pre class="codeinput">A1(k,k)=1;
A1(k,u)=-1;
A1(k,z)=1;
A2(k,kbar)=1;
A1(k,miu)=alpha/(1-alpha)+1;
</pre><h2>Eq. 3 Hours (L), from cost minimization<a name="21"></a></h2><pre class="codeinput">A1(L,Rk)=1;
A1(L,w)=-1;
A1(L,k)=1;
A1(L,L)=-1;
</pre><h2>Eq. 4 Return to Capital (Rk) from utilization FOC<a name="22"></a></h2><pre class="codeinput">A1(Rk,Rk)=1;
A1(Rk,u)=-chi;
</pre><h2>Eq. 5, Real Wages (w) PC (with split of shocks)<a name="23"></a></h2><pre class="codeinput">slopewages=(1-beta*xiw)*(1-xiw)/((1+beta)*xiw*(1+niu*(1+1/lambdaw)));
A1(w,w)=1;
A0(w,w)= -beta/(1+beta);
A0(w,p)= -beta/(1+beta);
A1(w,x)=slopewages;
A1(w,z)=(1+iotaw*beta-rhoz*beta)/(1+beta);
A1(w,miu) =(alpha/(1-alpha))*((1+iotaw*beta-rhomiu*beta)) *(1/(1+beta));
A1(w,p)=(1+iotaw*beta)/(1+beta);
A1(w,wmarkwn)= -1;

A2(w,p)=iotaw/(1+beta);
A2(w,w)=1/(1+beta);
A2(w,z)=iotaw/(1+beta);
A2(w,miu)=(alpha*iotaw)/( (1+beta)*(1-alpha) );
</pre><h2>Eq. 6, Wage Gap (x), (MRS-Real Wage)<a name="24"></a></h2><pre class="codeinput">A1(x,x)=1;
A1(x,w)=-1;
A1(x,lambda)=-1;
A1(x,b)=1/((1-rhob)*(expg-h*beta*rhob)*(expg-h)/(expg*h+expg^2+beta*h^2));
A1(x,L)=niu;
A1(x,psi)=1;
</pre><h2>Eq 7, Prices (p) PC<a name="25"></a></h2><pre class="codeinput">A1(p,p)=1;
A0(p,p)=-beta/(1+iotap*beta);
A2(p,p)=iotap/(1+iotap*beta);
A1(p,s)=-((1-beta*xip)*(1-xip)/((1+iotap*beta)*xip));
A1(p,lambdap)=-1;
</pre><h2>Eq 8, Marginal Cost (s)<a name="26"></a></h2><pre class="codeinput">A1(s,s)=1;
A1(s,Rk)=-alpha;
A1(s,w)=-(1-alpha);
</pre><h2>Eq 9, Lagrange Multiplier IBC (Lambda)<a name="27"></a></h2><pre class="codeinput">A1(lambda,lambda)=1;
A1(lambda,R)=-1;
A0(lambda,lambda)=-1;
A0(lambda,p)=1;
A1(lambda,z)=rhoz;
A1(lambda,miu)=(rhomiu)*alpha/(1-alpha);
</pre><h2>Eq 10, Consumption (c)<a name="28"></a></h2><pre class="codeinput">A1(c,lambda)=(expg-h*beta)*(expg-h);
A1(c,b)=-(expg-h*beta*rhob)*(expg-h)/((1-rhob)*(expg-h*beta*rhob)*(expg-h)/(expg*h+expg^2+beta*h^2));
A1(c,z)=-(beta*h*expg*rhoz-h*expg);
A1(c,miu)=(-(beta*h*expg*rhomiu-h*expg))*alpha/(1-alpha);
A1(c,c)=(expg^2+beta*h^2);
A0(c,c)=-beta*h*expg;
A2(c,c)=expg*h;
</pre><h2>Eq 11, Taylor Rule for Nominal Interest Rate (R)<a name="29"></a></h2><pre class="codeinput">A1(R,R)=1;
<span class="comment">% 10.1 Systematic Component</span>
A2(R,R)=rhoR;
A1(R,dpma)  = -(1-rhoR)*fp; <span class="comment">% Annualized Inflation</span>
A1(R,yobsan)= -(1-rhoR)*fy; <span class="comment">% Annualized Observable Output</span>
<span class="comment">% 10.2 Non-Systematic Component</span>
A1(R,mp)      = -1;         <span class="comment">% Contemporaneous Innovation</span>
A1(R,sigR_0)  = -1;         <span class="comment">% Signals</span>
A1(R,pitarg)  =(1-rhoR)*fp; <span class="comment">% Inflation Drift</span>
</pre><h2>Eq. 12, Utilization (U) from market clearing condition<a name="30"></a></h2><pre class="codeinput">A1(u,c)=css/yss;
A1(u,i)=iss/yss;
A1(u,y)=-1/gss;
A1(u,g)=1/gss;
A1(u,u)=kss*Rkss/yss;
</pre><h2>Eq. 13, Shadow Value of Capital (phi)<a name="31"></a></h2><pre class="codeinput">A1(phi,phi)=1;
A0(phi,phi)=-beta*exp(-gammastar-gammamiu)*(1-delta);
A1(phi,z)=rhoz;
A1(phi,miu)=(rhomiu)*(alpha/(1-alpha)+1);
A0(phi,lambda)=-(1-beta*exp(-gammastar-gammamiu)*(1-delta));
A0(phi,Rk)=-(1-beta*exp(-gammastar-gammamiu)*(1-delta));
</pre><h2>Eq. 14, Investment (i, in consumption units)<a name="32"></a></h2><pre class="codeinput">expgmiu=exp(gammastar+gammamiu);
A1(i,lambda)=1/(S*expgmiu^2);
A1(i,phi)=-1/(S*expgmiu^2);
A1(i,upsilon)=-1/(S*expgmiu^2);
A1(i,miu)=((1-beta*rhomiu))*(alpha/(1-alpha)+1); <span class="comment">%no normalization</span>
A1(i,i)=(1+beta);
A1(i,z)=(1-beta*rhoz);
A0(i,i)=-beta;
A2(i,i)=1;
</pre><h2>Eq 15, Capital accumulation (kbar)<a name="33"></a></h2><p>Note: Recall, effective capital above is KBAR*UTILIZATION</p><pre class="codeinput">A1(kbar,kbar)=1;
A1(kbar,i)=-(1-(1-delta)*exp(-gammastar-gammamiu));
A1(kbar,upsilon)=-(1-(1-delta)*exp(-gammastar-gammamiu));
A2(kbar,kbar)=(1-delta)*exp(-gammastar-gammamiu);
A1(kbar,z)=(1-delta)*exp(-gammastar-gammamiu);
A1(kbar,miu)=((1-delta)*exp(-gammastar-gammamiu))*(alpha/(1-alpha)+1);
</pre><h2>Eq 16, MP Exogenous Policy Innovation (unexpected)<a name="34"></a></h2><pre class="codeinput">A1(mp,mp)=1; A2(mp,mp)=rhomp; PSI(mp,Rs)=1;
</pre><h2>Eq 17, Exogenous Growth Rate in Neutral Technoloy<a name="35"></a></h2><pre class="codeinput">A1(z,z)=1; A2(z,z)=rhoz; PSI(z,zs)=1;
</pre><h2>Eq 18, Exogenous Process in G Spending<a name="36"></a></h2><pre class="codeinput">A1(g,g)=1; A2(g,g)=rhog; PSI(g,gs)=1;
</pre><h2>Eq 19, Exogenous Process in Investment Specific Tech or Relative Price C<a name="37"></a></h2><p>to I</p><pre class="codeinput">A1(miu,miu)=1; A2(miu,miu)=rhomiu; PSI(miu,mius)=1;
</pre><h2>Eq 20, Exogenous Process in Price Markup<a name="38"></a></h2><pre class="codeinput">A1(lambdap,lambdap)=1; A2(lambdap,lambdap)=rholambdap;PSI(lambdap,lambdaps)=1;
</pre><h2>Eq 21, Exogenous Process in Labor Disutility (AR1)<a name="39"></a></h2><pre class="codeinput">A1(psi,psi)=1; A2(psi,psi)=rhopsi; PSI(psi,psis)=1;
</pre><h2>Eq 22, Exogenous Process in Intertemporal Preference<a name="40"></a></h2><pre class="codeinput">A1(b,b)=1; A2(b,b)=rhob; PSI(b,bs)=1;
</pre><h2>Eq 23, Exogenous Process in MEI<a name="41"></a></h2><pre class="codeinput">A1(upsilon,upsilon)=1; A2(upsilon,upsilon)=rhoupsilon; PSI(upsilon,upsilons)=1;
</pre><h2>Eq 24, Exogenous Process in Inflation Drift<a name="42"></a></h2><pre class="codeinput">A1(pitarg,pitarg)=1; A2(pitarg,pitarg)=rhopitarg; PSI(pitarg,shpitarg)=1;
</pre><h2>Eq 25, Exogenous Process in Wage Markup (white noise)<a name="43"></a></h2><pre class="codeinput">A1(wmarkwn,wmarkwn)=1; PSI(wmarkwn,wmarkwnsh)=1;
</pre><h2>Eq. 26, Observable GDP Nominal (add P back)<a name="44"></a></h2><pre class="codeinput">A1(ynomobs,ynomobs) =  1;
A1(ynomobs,gdp)     = -1;
A1(ynomobs,dagtrend)= -1;
<span class="comment">% Add inflation, since nominal</span>
A1(ynomobs,p)       = -1;

A2(ynomobs,gdp)     = -1;
</pre><h2>Eq. 27, Observable Consumption Nominal (add P back)<a name="45"></a></h2><pre class="codeinput">A1(cnomobs,cnomobs) =1;
A1(cnomobs,c)       = -1;
A1(cnomobs,dagtrend)= -1;
<span class="comment">% Add inflation, since nominal</span>
A1(cnomobs,p)= -1;

A2(cnomobs,c)= -1;
</pre><h2>Eq. 28 Observable Investment Nominal (add P back)<a name="46"></a></h2><p>In consumption units</p><pre class="codeinput">A1(inomobs,inomobs)   =1;
A1(inomobs,i)         = -1;
A1(inomobs,dagtrend)  = -1;
<span class="comment">% Add inflation, since nominal</span>
A1(inomobs,p)= -1;

A2(inomobs,i)= -1;
</pre><h2>Eq. 29 Observable Hourly Wages  Nominal (add P back)<a name="47"></a></h2><pre class="codeinput">A1(wnomobs,wnomobs) =1;
A1(wnomobs,w)       = -1;
A1(wnomobs,dagtrend)= -1;
<span class="comment">% Add inflation, since nominal</span>
A1(wnomobs,p)= -1;

A2(wnomobs,w)= -1;
</pre><h2>Eq. 30 Observable Real GDP Growth (in C Units)<a name="48"></a></h2><p>Corrected observation equation for utilization <img src="modchimodDtFacpInfExp_eq00038.png" alt="$$ yrobs_{t} = gdp_{t}-gdp_{t-1} + agtrend_{t} $$"> where AGTREND is the aggregate trend</p><pre class="codeinput">A1(yrobs,yrobs)=1;
A1(yrobs,gdp)= -1;
A1(yrobs,dagtrend)= -1;
A2(yrobs,gdp)= -1;
</pre><h2>Eq. 31 GDP (detrended), Real Detrended Output corrected for Utilization<a name="49"></a></h2><pre class="codeinput">A1(gdp,gdp)= -1;
A1(gdp,y)=1;
A1(gdp,u)= -kss*Rkss/yss;
</pre><h2>Eq. 32 Growth Rate in Investment Deflator<a name="50"></a></h2><p><img src="modchimodDtFacpInfExp_eq69263.png" alt="$$ \Delta P^{I}_{t} = \Delta P^{C}_{t} - \mu_{t} $$"></p><pre class="codeinput">A1(dpinv,dpinv)= -1;
A1(dpinv,p)    =  1;
A1(dpinv,miu)  = -1;
</pre><h2>Eq. 33 GDP Deflator<a name="51"></a></h2><p><img src="modchimodDtFacpInfExp_eq07163.png" alt="$$ P^{GDP}_{t} = \beta_{1} P^{C}_{t} + \beta_{2} P^{I}_{t} $$"></p><p>Recall there are 2 ways to define the weights</p><p><b>First:</b> <img src="modchimodDtFacpInfExp_eq81438.png" alt="$$ {\frac{C}{C+I} , \frac{I}{C+I} } $$"></p><p><b>Second:</b> <img src="modchimodDtFacpInfExp_eq46087.png" alt="$$ {\frac{C}{Y}  , \frac{I}{Y}   } $$"></p><p>Note that in second case weights will not add up to one, which is not desirable, but opens up the door for adding a 3 deflator, i.e. price of G and NX.</p><pre class="codeinput">bet_gdpdef=bet_gdpdef(:);
weights=zeros(1,2);
<span class="keyword">if</span> flag_wadd1==1
    weights(1)=css/(css+iss);
    weights(2)=iss/(css+iss);
<span class="keyword">else</span>
    weights(1)=css/yss;
    weights(2)=iss/yss;
<span class="keyword">end</span>
A1(gdpdef,gdpdef) = -1;
A1(gdpdef,p)      = weights(1)*bet_gdpdef(1);
A1(gdpdef,dpinv)  = weights(2)*bet_gdpdef(2);
</pre><h2>Eq. 34 Lag 1 Observable Real GDP Growth (in C Units)<a name="52"></a></h2><pre class="codeinput">A1(yrobs_1,yrobs_1)=1; A2(yrobs_1,yrobs)=1;
</pre><h2>Eq. 35 Lag 2 Observable Real GDP Growth (in C Units)<a name="53"></a></h2><pre class="codeinput">A1(yrobs_2,yrobs_2)=1; A2(yrobs_2,yrobs_1)=1;
</pre><h2>Eq. 36 Annualized Observable Real GDP Growth (in C Units)<a name="54"></a></h2><p><img src="modchimodDtFacpInfExp_eq61717.png" alt="$$ \Delta y^{A}_{t}= \sum_{j=0}^{3}yrobs_{t-j} $$"></p><pre class="codeinput">A1(yobsan,yobsan)= -1;

A1(yobsan,yrobs)  =1;
A1(yobsan,yrobs_1)=1;
A1(yobsan,yrobs_2)=1;
A2(yobsan,yrobs_2)= -1;
</pre><h2>Eq. 37 Lag 1 Inflation (C)<a name="55"></a></h2><pre class="codeinput">A1(p_1,p_1)=1; A2(p_1,p)=1;
</pre><h2>Eq. 38 Lag 2 Inflation (C)<a name="56"></a></h2><pre class="codeinput">A1(p_2,p_2)=1; A2(p_2,p_1)=1;
</pre><h2>Eq. 39 Annualized Inflation (C)<a name="57"></a></h2><p><img src="modchimodDtFacpInfExp_eq58391.png" alt="$$ \Delta P^{A,C}_{t}= \frac{ \sum_{j=0}^{3} \Delta P^{C}_{t-j} }{4} $$"></p><pre class="codeinput">A1(dpma,dpma)= -1;
A1(dpma,p)   = 1/4;
A1(dpma,p_1) = 1/4;
A1(dpma,p_2) = 1/4;
A2(dpma,p_2) = -1/4;
</pre><h2>Eq. 40, Growth Rate in Aggregate Trend<a name="58"></a></h2><p><img src="modchimodDtFacpInfExp_eq56094.png" alt="$$ \Gamma_{t} = Z_{t} + \frac{\alpha}{1-\alpha} \mu_{t} $$"></p><pre class="codeinput">A1(dagtrend,dagtrend)= -1;
A1(dagtrend,z)=1;
A1(dagtrend,miu)=alpha/(1-alpha);
</pre><h2>Eq. 45-49 Monetary Policy Signals<a name="59"></a></h2><h2>Eq. 45. <img src="modchimodDtFacpInfExp_eq34073.png" alt="$$ \zeta^{4}_{t}       = \epsilon^{4}_{t} $$"><a name="60"></a></h2><pre class="codeinput">A1(sigR_4,sigR_4)=1;PSI(sigR_4,Rs_lag4)=1;
</pre><h2>Eq. 46.  <img src="modchimodDtFacpInfExp_eq08731.png" alt="$$ \zeta^{3}_{t} = \zeta^{4}_{t-1} + \epsilon^{3}_{t} $$"><a name="61"></a></h2><pre class="codeinput">A1(sigR_3,sigR_3)=1; A2(sigR_3,sigR_4)=1; PSI(sigR_3,Rs_lag3)=1;
</pre><h2>Eq. 47.  <img src="modchimodDtFacpInfExp_eq75028.png" alt="$$ \zeta^{2}_{t} = \zeta^{3}_{t-1} + \epsilon^{2}_{t} $$"><a name="62"></a></h2><pre class="codeinput">A1(sigR_2,sigR_2)=1; A2(sigR_2,sigR_3)=1; PSI(sigR_2,Rs_lag2)=1;
</pre><h2>Eq. 48.  <img src="modchimodDtFacpInfExp_eq04683.png" alt="$$ \zeta^{1}_{t} = \zeta^{2}_{t-1} + \epsilon^{1}_{t} $$"><a name="63"></a></h2><pre class="codeinput">A1(sigR_1,sigR_1)=1; A2(sigR_1,sigR_2)=1; PSI(sigR_1,Rs_lag1)=1;
</pre><h2>Eq. 49. <img src="modchimodDtFacpInfExp_eq01231.png" alt="$$ \zeta^{0}_{t} = \zeta^{1}_{t-1} $$">, which implies that the history of<a name="64"></a></h2><p>policy signals regarding the current quarter is given by</p><p><img src="modchimodDtFacpInfExp_eq64807.png" alt="$$ \zeta^{0}_{t} = \epsilon^{1}_{t-1} +  \epsilon^{2}_{t-2} + \epsilon^{3}_{t-3} + \epsilon^{4}_{t-4}   $$"> <b>Note:</b> The contemporaneous policy innovation appears directly in the definition of the Taylor rule, in case it is AR(1). An alternative would be to make the current composite signal + contemporaneous innovation persistent.</p><pre class="codeinput">A1(sigR_0,sigR_0)=1;A2(sigR_0,sigR_1)=1;
</pre><pre class="codeinput">pe_vec=pe_vec(:);
A1(pe_vec,pe_vec)=eye(length(pe_vec));
A0(pe_vec,[p;pe_vec(1:end-1)])= -eye(length(pe_vec));

A1(expectedPi40,expectedPi40)= -1;
A1(expectedPi40,pe_vec      )= (expInfLoading/length(pe_vec))*ones(1,length(pe_vec));
</pre><h2><b>PART 7: Solve Model using AMASOLVE</b><a name="66"></a></h2><pre class="codeinput">[GG,~,impact,~,~,~,~,eu]=amasolve(A0,A1,A2,zeros(NY,1),PSI) ;
<span class="comment">% EU =[1;1] if unique and stable RE solution</span>
<span class="keyword">if</span> ~isequal(eu,[1;1]);
    flag.euok=0;
    ZZ=[]; C=[]; SDX=[];
    <span class="keyword">return</span>
<span class="keyword">end</span>

flag.euok=1;
</pre><h2><b>PART 8: Addition of Measurement Equations for Prices</b><a name="67"></a></h2><h2>8.1 Extend model solution by 2*NSERP rows<a name="68"></a></h2><p>Number of rows to add to the solution</p><pre class="codeinput">nadd=nserp*2;
<span class="comment">% Part 1: Position of prices</span>
posp =(NY+1):(NY+nserp);
<span class="comment">% Part 2: Position of ID errors</span>
posid=(posp(end)+1):(NY+2*nserp);
GG=blkdiag(GG,zeros(nadd));
<span class="comment">% Matrix of impact coefficients</span>
RR=zeros(NY+nadd,NX+nserp);
RR(1:NY,1:NX)=impact;
</pre><h2>8.2 Idiosyncratic errors<a name="69"></a></h2><p>Which evolve as <img src="modchimodDtFacpInfExp_eq96656.png" alt="$$ \nu^{i}_{t} = \rho  \nu^{i}_{t-1} + \varsigma^{i}_{t} $$"> as independent AR(1) processes</p><pre class="codeinput">GG(posid,posid   )=diag(rhoid);
RR(posid,NX+1:end)=eye(nserp);
</pre><h2>8.3 General Structure of Observable Price Equation<a name="70"></a></h2><p>For price <img src="modchimodDtFacpInfExp_eq38331.png" alt="$$ P^{i,C}_{t} = \beta^{i} P^{C}_{t} + \nu^{i}_{t} $$"> written in terms of lagged variables as <img src="modchimodDtFacpInfExp_eq58514.png" alt="$$ P^{i,C}_{t} = \beta^{i} G_{P^{C}} S_{t-1} + \beta^{i} R_{P^{C}} \eta_{t} + \rho \nu^{i}_{t-1} + \varsigma^{i}_{t} $$"> , where <img src="modchimodDtFacpInfExp_eq30964.png" alt="$$ \beta^{i} $$"> is the series specific factor loading, and, <img src="modchimodDtFacpInfExp_eq59630.png" alt="$$ G_{P^{C}}  R_{P^{C}} $$"> correspond to the rows of the solution matrix for the model consumption price</p><h2>8.3.a First row is GDP<a name="71"></a></h2><pre class="codeinput">GG(posp(1),1:NY)=GG(gdpdef,1:NY);
RR(posp(1),1:NX)=RR(gdpdef,1:NX);
</pre><h2>8.3.b Remaining rows are multiple indicators for consumption prices<a name="72"></a></h2><p>Recall Structural part of observation equation <img src="modchimodDtFacpInfExp_eq05086.png" alt="$$ \beta^{i} G_{P^{C}} S_{t-1} + \beta^{i} R_{P^{C}} \eta_{t}  $$"></p><pre class="codeinput">GG(posp(2:end),:)       =repmat( GG(p,:)    ,[nserp-1 1] );
RR(posp(2:end),1:NX)    =repmat( RR(p,1:NX) ,[nserp-1 1] );
<span class="comment">% Multiply by Factor Loadings</span>
bet_c=bet_c(:);
GG(posp(2:end),1:NY)=(repmat(bet_c,[1 NY])).*GG(posp(2:end),1:NY);
RR(posp(2:end),1:NX)=(repmat(bet_c,[1 NX])).*RR(posp(2:end),1:NX);
</pre><h2>8.3.c Add idiosyncratic errors<a name="73"></a></h2><pre class="codeinput">GG(posp,posid)   =diag(rhoid);
RR(posp,NX+1:end)=eye(nserp);

<span class="comment">% Update model dimensions after adding prices</span>
impact=RR;
NY=size(GG,1);
NX=size(RR,2);
</pre><h2><b>Part 9: Addition of Measurement Equations for Spread</b><a name="74"></a></h2><h2>9.1 Extend model solution by 1 row<a name="75"></a></h2><pre class="codeinput">GG    =blkdiag(GG,0);
impact=[impact zeros(NY,1);zeros(1,NX+1)];
</pre><h2>9.2 Fill in last row for the spread<a name="76"></a></h2><p><img src="modchimodDtFacpInfExp_eq77724.png" alt="$$ sp_{t} = \kappa \upsilon_{t} + \varsigma^{sp}_{t} $$"></p><pre class="codeinput">posspread=NY+1;
GG(posspread,:)       =kappa*GG(upsilon,:);
impact(posspread,:)   =kappa*impact(upsilon,:);
impact(posspread,NX+1)=1;

<span class="comment">% Update model dimensions after adding spread</span>
NY=NY+1;
NX=NX+1;
</pre><h2><b>PART 10: Declare Cholesky of Variance Covariance Matrix</b><a name="77"></a></h2><p>Recall <img src="modchimodDtFacpInfExp_eq26143.png" alt="$$ V = SDX'SDX $$"> and the order of the shocks is 1. Structural 2. Policy Signals 3. Idiosyncratic Prices 4. Measurement Error Spread</p><pre class="codeinput">SDX=diag([sdstruct(:);stdsignals(:);sdid(:);sdspreadme]);
<span class="keyword">if</span> size(SDX,1)~=NX;error(<span class="string">'Dimensions of errors and SDX do not match'</span>);<span class="keyword">end</span>
</pre><h2><b>PART 11: Declare ZZ, Matrix of Pointers to Observables</b><a name="78"></a></h2><p>Ordering 1) Nominal GDP Growth. 2) Nominal C   Growth. 3) Nominal I   Growth. 4) Hours. 5) Nominal W   Growth. 6) Feds Fund Rate. 7) Investment Deflator. 8) GDP Deflator. 9 though NSERP-1) Indicators of C Prices. Last is Spread</p><pre class="codeinput">dimZ=7+nserp+1+1;
ZZ=zeros(dimZ,NY);
ZZ(1,ynomobs)=1;
ZZ(2,cnomobs)=1;
ZZ(3,inomobs)=1;
ZZ(4,L)=1;
ZZ(5,wnomobs)=1;
ZZ(6,R)=1;
ZZ(7,dpinv)=1;
ZZ(8:dimZ-2,posp)=eye(nserp);
ZZ(dimZ-1,posspread)=1;
ZZ(dimZ  ,expectedPi40)=1;
</pre><h2><b>PART 12: Declare C, Matrix of Constants</b><a name="79"></a></h2><pre class="codeinput">C=zeros(NY,1);
C(ynomobs)=gammastar100+pss100;
C(cnomobs)=gammastar100+pss100;
C(inomobs)=gammastar100+pss100;
C(L)=Lss;
C(wnomobs)=gammastar100+pss100;
C(R)    =pss100+rss100;
C(dpinv)=(pss100-gammamiu100)+cons_idef;
C(posp )= pss100*ones(nserp,1)+cprices(:);
C(expectedPi40)=pss100+constantCPI;

<span class="keyword">if</span> flag_repstanames==0;
    <span class="keyword">return</span>
<span class="keyword">end</span>
<span class="comment">%________________________________________________________</span>
</pre><h2>16.1 STANAMES: Cell with Names of ALL states<a name="80"></a></h2><pre class="codeinput">stanames1={<span class="string">'yhat'</span>,<span class="string">'khat'</span>,<span class="string">'hours'</span>,<span class="string">'Return K'</span>,<span class="string">'what'</span>,<span class="string">'x'</span>,<span class="string">'dp'</span>,<span class="string">'Marginal Cost'</span>,<span class="string">'lambdahat'</span>,<span class="string">'chat'</span>,<span class="keyword">...</span>
    <span class="string">'nomr'</span>,<span class="string">'Utilization '</span>,<span class="string">'phihat'</span>,<span class="string">'ihat'</span>,<span class="string">'kbar'</span>,<span class="string">'mp'</span>,<span class="string">'z'</span>,<span class="string">'g'</span>,<span class="string">'miu'</span>,<span class="string">'lambdap'</span>,<span class="keyword">...</span>
    <span class="string">'psi'</span>,<span class="string">'b'</span>,<span class="string">'upsilon '</span>,<span class="string">'pitarg'</span>,<span class="string">'wmarkwn'</span>,<span class="string">'GDPm nominal'</span>,<span class="string">'Cm nominal'</span>,<span class="string">'Im nominal'</span>,<span class="string">'Wm nominal'</span>,<span class="keyword">...</span><span class="comment">  % Position 29</span>
    <span class="string">'GDPm real'</span>,<span class="string">'GDPm level'</span>,<span class="string">'I deflator'</span>,<span class="string">'GDPm deflator'</span>,<span class="string">'GDPm real lag1'</span>,<span class="string">'GDP real lag2'</span>,<span class="string">'GDP Annualized'</span>,<span class="string">'Inflation lag1'</span>,<span class="string">'Inflation lag2'</span>,<span class="string">'Inflation annualized'</span>,<span class="string">'Aggregate trend'</span>,<span class="keyword">...</span>
    <span class="string">'Signal0'</span>,<span class="string">'Signal1'</span>,<span class="string">'Signal2'</span>,<span class="keyword">...</span>
    <span class="string">'Signal3'</span>,<span class="string">'Signal4'</span>,<span class="keyword">...</span>
    <span class="string">'ExpInf1'</span>,<span class="string">'ExpInf2'</span>,<span class="string">'ExpInf3'</span>,<span class="string">'ExpInf4'</span>,<span class="string">'ExpInf5'</span>,<span class="string">'ExpInf6'</span>,<span class="string">'ExpInf7'</span>,<span class="string">'ExpInf8'</span>,<span class="string">'ExpInf9'</span>,<span class="string">'ExpInf10'</span>,<span class="keyword">...</span>
    <span class="string">'ExpInf11'</span>,<span class="string">'ExpInf12'</span>,<span class="string">'ExpInf13'</span>,<span class="string">'ExpInf14'</span>,<span class="string">'ExpInf15'</span>,<span class="string">'ExpInf16'</span>,<span class="string">'ExpInf17'</span>,<span class="string">'ExpInf18'</span>,<span class="string">'ExpInf19'</span>,<span class="string">'ExpInf20'</span>,<span class="keyword">...</span>
    <span class="string">'ExpInf21'</span>,<span class="string">'ExpInf22'</span>,<span class="string">'ExpInf23'</span>,<span class="string">'ExpInf24'</span>,<span class="string">'ExpInf25'</span>,<span class="string">'ExpInf26'</span>,<span class="string">'ExpInf27'</span>,<span class="string">'ExpInf28'</span>,<span class="string">'ExpInf29'</span>,<span class="string">'ExpInf30'</span>,<span class="keyword">...</span>
    <span class="string">'ExpInf31'</span>,<span class="string">'ExpInf32'</span>,<span class="string">'ExpInf33'</span>,<span class="string">'ExpInf34'</span>,<span class="string">'ExpInf35'</span>,<span class="string">'ExpInf36'</span>,<span class="string">'ExpInf37'</span>,<span class="string">'ExpInf38'</span>,<span class="string">'ExpInf39'</span>,<span class="string">'ExpInf40'</span>,<span class="string">'AvgExpInf10y'</span>,<span class="keyword">...</span>
    <span class="string">'GDP deflator'</span>};
<span class="keyword">if</span> nserp==3
    stanames2={<span class="string">'C deflator'</span>,<span class="string">'PCE Core'</span>,<span class="string">'GDP def Id'</span>,<span class="string">'C def Id'</span>,<span class="string">'PCE Core Id'</span>};
<span class="keyword">elseif</span> nserp==4
    stanames2={<span class="string">'C deflator'</span>,<span class="string">'PCE Core'</span>,<span class="string">'CPI Core'</span>,<span class="string">'GDP def Id'</span>,<span class="string">'C def Id'</span>,<span class="string">'PCE Core Id'</span>,<span class="string">'CPI Core Id'</span>};
<span class="keyword">else</span>
    disp(<span class="string">'Nprices &gt; 4 please add names to STANAMES'</span>)
<span class="keyword">end</span>
stanames3={<span class="string">'Spread'</span>}; <span class="comment">%,'Spread Me','RealY','RealC','RealI','RealW','SumR'};</span>
stanames=[stanames1(:);stanames2(:);stanames3(:)];
</pre><h2>16.2 STSHOCKS: Cell with Names of Shocks in STANAMES<a name="81"></a></h2><p><b>Note:</b> Technically not true for the policy signals, for which looking only at the cummulative signals. Hence isolate policy innovations rather than policy states.</p><pre class="codeinput">stshocks={<span class="string">'mp'</span>,<span class="string">'z'</span>,<span class="string">'g'</span>,<span class="string">'miu'</span>,<span class="string">'lambdap'</span>,<span class="string">'psi'</span>,<span class="string">'b'</span>,<span class="string">'upsilon '</span>,<span class="string">'pitarg'</span>,<span class="string">'wmarkwn'</span>,<span class="keyword">...</span>
    <span class="string">'Signal1'</span>,<span class="string">'Signal2'</span>,<span class="string">'Signal3'</span>,<span class="string">'Signal4'</span>,<span class="keyword">...</span>
    <span class="string">'GDP def Id'</span>,<span class="string">'C def Id'</span>,<span class="string">'PCE Core Id'</span>};
<span class="keyword">if</span> nserp==4
    stshocks=[stshocks,<span class="string">'CPI Id'</span>];
<span class="keyword">end</span>
stshocks=[stshocks,<span class="string">'Spread Me'</span>];
stshocks=stshocks(:);
</pre><h2>16.3 SHONAMES: Cell with Names of Shock Innovations<a name="82"></a></h2><pre class="codeinput">shonames={<span class="string">'MP'</span>,<span class="string">'Neutral'</span>,<span class="string">'G+NX'</span>,<span class="string">'ISTS'</span>,<span class="string">'Price Markup'</span>,<span class="string">'Labor Disutility'</span>,<span class="keyword">...</span>
    <span class="string">'Discount'</span>,<span class="string">'MEI'</span>,<span class="string">'Inflation Drift'</span>,<span class="string">'Wage Markup'</span>,<span class="keyword">...</span>
    <span class="string">'MP Signal 1'</span>,<span class="string">'MP Signal 2'</span>,<span class="string">'MP Signal 3'</span>,<span class="string">'MP Signal 4'</span>};
shonames=[shonames(:);stshocks(15:end)];


<span class="comment">%obsnames={}; for ii=1:size(ZZ,1); temp=stanames( find(ZZ(ii,:)~=0 ) ); obsnames=[obsnames;temp(:)]; end;</span>
</pre><h2>16.4 Quick check dimensions<a name="83"></a></h2><pre class="codeinput"><span class="keyword">if</span> length(stanames)~=size(GG,1)
    error(<span class="string">'Mistmatch STANAMES and NY'</span>);
<span class="keyword">end</span>
<span class="keyword">if</span> length(stshocks)~=size(SDX,1)
    error(<span class="string">'Mistmatch STSHOCKS and NX'</span>);
<span class="keyword">end</span>
<span class="keyword">if</span> length(shonames)~=size(SDX,1)
    error(<span class="string">'Mistmatch SHONAMES and NX'</span>);
<span class="keyword">end</span>
</pre><p class="footer"><br>
      Published with MATLAB&reg; 7.11<br></p></div><!--
##### SOURCE BEGIN #####
 function [GG,impact,C,eu,SDX,ZZ,initss,ssvec,flag,ssnames,stanames,shonames]=modchimodDtFacpInfExp(parest,options,op,calval,posest,poscal)
% =========================================================================
%
% Last Modified July 25 2011. Compatibility with code ion 
%
% function [GG,C,impact,eu,SDX,ZZ,NY,NX,NETA,gev,ncof]=...
% modchimodDtFacpXSpreadNews(parest,calval,posest,poscal)
%
%% *I. Description for CHIMOD DT FACP X SPREAD NEWS4* 
% 
% Model code for Chicago Fed model (CHIMOD) with 
% Double Trend (DT) Factor structure for prices (FACP) 
% Separates the ARMA(1,1) wage markup/labor disutility shock into a 
% white noise wage markup and an AR(1) labor disutility shock (X) 
% with a spread observable (SPREAD) and allowing for policy news shocks
% upto 4 periods ahead (NEWS4). 
% 
%% *II. Input* 
%
% There are either 1 or 4 inputs 
% *CASE 1* If there is 1 input, then PAREST should be the whole parameter
% vector. 
% 
% *CASE 2* If there are 4 inputs, the parameter vector PARAM is created
% from the estimated and calibrated values. 
% PAREST are the estimated values in rows POSEST and CALVAL are the
% calibrated values in position POSCAL. 
%
%% *III. Output*  
%
% The model to be solved is written, inside this code, as $$ A_{0}Y_{t+1} +
% A_{1}Y_{t}=A_{2}Y_{t-1} + PSI \eta_{t} $$  where the A matrices are
% filled with the linearized equation and entries depend on the parameter
% vector PARAM. 
%
% The solution of the model is a state space system characterized by 
%
% *Transition Equation* $$ S_{t} = GG S_{t-1} + RR \eta_{t} $$ and variance covariance matrix, 
% where $$ S_{t} $$ is the vector of all
% variables (endogenous and exogenous) of the model of dimension [NY 1],
% and $$ \eta $$  is the vector of exogenous innovations of dimension [NX 1] with 
% variance covariance matrix $$ V(\eta) = SDX'SDX $$. 
% Matrix dimensions: 
% GG [NY NY], IMPACT [NY NX], SDX [NX NX].  
%
% *Measurement Equation* $$ Y_{t} = ZZ S_{t} + ZZ C  $$ , 
% where $$ Y_{t} $$ is the data vector of dimension [NZ 1], $$ ZZ $$ is a selection matrix indicating 
% which states are observed, and $$ CC $$ is a vector of constants of
% dimension [NY 1]. 
% 
% EU is a [2 1] vector equal to [1;1] if there is a unique stable RE
% solution 
% 
% NETA GEV and COF are obsolete outputs that are still called in some codes
% and will be deleted in the new streamlined version of the estimation
% routine. 
%
%% *IV Notes on Multiple Prices* 
% 
% This version accomodates as observables an Investment Deflator and an additional NSERP prices. 
% Of these NSERP prices the first one should be the GDP deflator and the
% remaining NSERP-1 series multiple observables for the Consumption
% Deflator (e.g. chain weighted model consistent, Core PCE, Core CPI). 
% 
%% *V. Notes on Parametrization* 
% 1. Taylor rule written in terms of annualized output and inflation
% growth, with an inflation drift. 
% 2. Wage markup and labor disutility shock split. 
% 3. The composite steady state (SS) growth rate of the trend and the (SS)
% growth rate of ISTS progress are controlled by separate parameterss
% *gamma100* and *gammamiu100* rather than two parameters connected with
% ALPHA. This facilitates putting priors. 
% 4. AR coefficients of idiosyncratic errors are given by 2*PAR-1, where
% PAR is the paramater plugged into solution. Hence while PAR should be in
% the [0,1] interval, the AR coefficients are allowed to go negative. 
% 
%% *IV.a Model and data means* 
% 
% The model based means in the observation equation are given by the
% parameters *gammamiu100* and *pss100* which correspond to the mean growth
% rate of the relative price of investment to consumption and the mean
% growth rate of the consumption deflator. In addition, we allow the means
% of the observed price series to differ from these model based means
% according to extra parameters, intended to capture the discrepancy
% between model and data. 
% 
% Hence the model based mean of the Investment deflator is obtained as
% *pss100-gammamiu100-CMIU*.
% Similarly, the constants for the Consumption prices  is given by
% *pss100+CP* where CP differs by price. For simplicity this is also the
% constant for the GDP deflator, rather than aggregating that for
% investment and consumption; historically for instance Core PCE and the
% GDP deflator have grown at very similar rates. 
% 
%% *V. Related models* 
% 
% *MOD CHIMOD DT FAC P3 X SPREAD NEWS4 _ OBS:* is used to declare
% additional variables not used in estimation but annalized (e.g. output
% gap, real rate). 
% 
% *MOD CHIMOD DT FAC P3 SPREAD NEWS4:* very similar code but without split
% of wage markup and labor distulity shocks 
% 
%% *Version*
% Created by Alejandro Justiniano 12/13/2010  
% 
% Last Modified 12/13/2010. Run code ZB_CHECKWAGESPC to ensure alternative
% form of the wage Phillips curve matches model without split of wage markup and labor
% disutility shock. Also IRF of I to neutral shock is reasonable. 
% =========================================================================

if nargout < 11 
    flag_repstanames=0; 
else 
    flag_repstanames=1; 
end 
if nargout < 10 
    flag_repssnames=0; 
else
    flag_repssnames=1; 
end

flag.ssok=1;
flag.solver=1; 

initss=[]; 
ssnames=[]; 
ssvec  =[]; 


% if nargin~=1 && nargin~=4 
%     error('Need either 1 or 4 inputs') 
% end 

% =========================================================================
%% *PART 1. Declare All Variables in the Model Solution $$ S_{t} $$* 

%__________________________________________________________________________
% 1.a Endogenous Variables 
y=1;            % output
k=2;            % capital
L=3;            % hours
Rk=4;           % return on capital
w=5;            % real wages
x=6;            % marginal utility of labor
p=7;            % inflation
s=8;            % marginal cost
lambda=9;       % multiplier
c=10;           % consumption
R=11;           % interst rate
u=12;           % capital utilization
phi=13;         % multiplier
i=14;           % investment
kbar=15;        % "gross" capital
%__________________________________________________________________________
% 1.b Exogenous driving processes
mp=16;
z =17;          % Productivity shock
g =18;          % Public spending shock
miu=19;         % Relative price (non stationary), this is Growth Rate
lambdap=20;     % Markup shock AR(1) 
psi=21;         % Preference shock AR(1) 
b=22;           % Discount factor shock 
upsilon=23;     % MEI, stationary, investment shock
pitarg=24;      % Inflation Drift
wmarkwn=25;     % White Noise Wage Markup 
%__________________________________________________________________________
% 1.c Observable growth rates (nominal) 
ynomobs=26; 
cnomobs=27;
inomobs=28; 
wnomobs=29; 
%__________________________________________________________________________
% 1.c.a Auxiliary Variables or Observable Prices
yrobs   =30; 
gdp     =31; 
dpinv   =32; 
gdpdef  =33; 
%__________________________________________________________________________
% 1.d Lags
yrobs_1=34;   % Real Observable Output (adjusted for utilization) lag1      
yrobs_2=35;   % Real Observable Output (adjusted for utilization) lag2
yobsan =36;   % Annualize Observable Output 
p_1    =37; 
p_2    =38; 
dpma   =39;   % Four quarter inflation  
dagtrend=40;  % First difference of the aggregate trend 
%__________________________________________________________________________
% 1.e 1 through 4 step ahead expectations of FFR 
startstar=40;
% 1.f Policy signals
stsig=startstar; 
sigR_0=stsig+1; 
sigR_1=stsig+2; 
sigR_2=stsig+3; 
sigR_3=stsig+4; 
sigR_4=stsig+5; 
% 1.g Inflation Expectations 
posExpInf=stsig+5+1; 
%% Expectations of inflation 1 through 40 quarters ahead
pe_vec=(posExpInf):(posExpInf+39); 
expectedPi40=posExpInf+40; 
NY=posExpInf+40;

% =========================================================================

% =========================================================================
%% *PART 2. Declare Innovations to the exogenous variables $$ \eta_{t} $$* 

nsignals = 4;    % Number of policy signals 
NX=10+ nsignals; 
%__________________________________________________________________________
% 2.a Structural Disturbances 
Rs=1;           % MP
zs=2;           % Technology 
gs=3;           % Public spending
mius=4;         % Investment     
lambdaps=5;     % Mark-up
psis=6;         % Leisure preference
bs=7;           % Discount factor
upsilons=8;     % investment shock
shpitarg=9;     % Inflation Drify
wmarkwnsh=10; 
%__________________________________________________________________________
% 2.b Policy Signals 
Rs_lag1=11;    % 1 period ahead
Rs_lag2=12;    % 2 periods ahead
Rs_lag3=13;    % 3 periods ahead
Rs_lag4=14;    % 4 periods ahead
% =========================================================================

% =========================================================================
%% *PART 3. Declare Parameters* 
ncof=        68;    % Number of coefficients 
numpar=      ncof;  % Equal to Parameters, distinction irrelevant here 
if nargin < 4 || isempty( calval ) 
    param=parest  ; 
else
    param=zeros(numpar,1); 
    param(posest)=parest; 
    param(poscal)=calval; 
end; 
%__________________________________________________________________________
% 3.1 Parameters for all but multiple prices, GDP and I deflators 

alpha=param(1);         % share of capital in the prod. function
delta=param(2);         % capital depreciation rate
iotap=param(3);         % price indexation (=0 is static indexation, =1 is dynamic)
iotaw=param(4);         % wages indexation
gammastar100=param(5);  % steady state growth rate of technology
gammamiu100=param(6);   % steady state growth rate of capital embodied technology
h=param(7);             % habit formation parameter
lambdapss=param(8);     % steady state of mark-up shock (pins down steady state of wages)
lambdaw=param(9);       % wage mark-up
Lss=param(10);          % steady state for leisure (the ss for leisure is pinned down by psiss. convenient to parameterize the model in terms of Lss)
pss100=param(11);       % steady state quarterly inflation (multiplied by 100)
Fbeta=param(12);        % weird parameter of SW that is a function of beta and the steady state quarterly real rate of interest (ensures that beta is less than 1)
gss=param(13);          % steady state of the public spending shock
niu=param(14);          % curvature of the utility function for leisure
xip=param(15);          % price stickiness
xiw=param(16);          % wage stickiness
chi=param(17);          % elasticity of the capital utilization cost function
S=param(18);            % investment adjustment cost
fp=param(19);           % reaction to annualized inflation
fy=param(20);           % reaction to annualized output growth 
rhoR=param(21);         % policy smoothing  
% AR coefficients of exogenous prorcesses 
rhoz=param(22); 
rhog=param(23); 
rhomiu=param(24); 
rholambdap=param(25); 
rhopsi=param(26); 
rhob=param(27); 
rhopitarg=param(28); 
rhomp=param(29); 
rhoupsilon=param(30); 
% Std of structural shocks 
sdstruct=param(31:40);
kappa       = -param(41);   % Scaling for spredd  
sdspreadme  =  param(42);   % Measurement error spread
% Std Policy Signals
stdsignals  =param(43:46); 
if length(stdsignals)~=nsignals 
    error('Wrong STD for signals') 
end 
%% Parameters for inflation expectations 
posCPI=param(47); 
expInfLoading=param(48); 
end_nonp=48; 
% _________________________________________________________________________
% 3.2  Begin Parameters for Multiple Prices 
%__________________________________________________________________________
% 3.2.1  How to aggregate gdp deflator
flag_wadd1  =param(end_nonp+1); 
nserp       =param(end_nonp+2); 
% __________________________________________________________________
% 3.2.1 Declare positions for 
% Rows for 
% a) loadings 
% b) ar ID errors 
% c) constants 
% d) volatilities 
%
% *Note:* There are NSERP+1 loadings. First 2 are for the GDP Deflator.
% 1st of W(1)*Price consumption.2nd on W(2)*Prince Investment. 
% Remaining are for other prices on the Consumption Deflators 
row_load=(end_nonp+3):(end_nonp+nserp+3); 
row_ar  =row_load(end)*ones(1,nserp)+(1:nserp); 
row_std =row_ar(end)*ones(1,nserp)+(1:nserp);
row_c   =row_std(end)*ones(1,nserp)+(1:nserp); 

%__________________________________________________________________________
% 3.2.3 vector of factor loadings 
bet_gdpdef  =param(row_load(1:2)); 
bet_c       =param(row_load(3:end)); 

bet_CPI     =bet_c(posCPI-1); 
if abs(expInfLoading-999) < 0.01; 
    expInfLoading=bet_CPI;
end



%__________________________________________________________________________
% 3.2.4 vector of idiosyncratic errors 
rhoid=2*param(row_ar)-1; 
if any(rhoid < -0.995)~=0 || any(rhoid > 0.995)~= 0 
    disp('Explosive AR ID roots'); 
    GG=[];C=[];impact=[];eu=[0;0];SDX=[];ZZ=[];NY=[];NX=[];NETA=[];gev=[];ncof=[];
    return
end

%__________________________________________________________________________
% 3.2.5 vector of constants 
cprices =param(row_c); 
if all( cprices ~= 0 ) 
    error('At least one of the constants must be set equal to 0')
end
constantCPI=cprices(posCPI); 

%__________________________________________________________________________
% 3.2.6 vector of idiosyncratic errors
sdid             =param(row_std); 

%__________________________________________________________________________
% 3.2.7 constants for I deflator 
cons_idef        =param(row_c(end)+1:end);
if length(cons_idef)> 1 
    error('Wrong Dimension of Investment Deflator') 
end 

%==========================================================================
%% *PART 4. Compute Steady State (SS)* 
 
gammastar=gammastar100/100;
gammamiu=gammamiu100/100;
beta=100/(Fbeta+100);
rss=exp(gammastar)/beta-1;
rss100=rss*100;
gss=1/(1-gss);
Rkss=exp(gammastar+gammamiu)/beta-1+delta;
sss=1/(1+lambdapss);
wss=(sss*((1-alpha)^(1-alpha))/((alpha^(-alpha))*Rkss^alpha))^(1/(1-alpha));
kLss=(wss/Rkss)*alpha/(1-alpha);
FLss=(kLss^alpha-Rkss*kLss-wss);
yLss=kLss^alpha-FLss;

% Scale of the economy is fixed at 1, i.e. independent of LSS which now
% only enters observation equation for hours 
Lsscale=1;
kss=kLss*Lsscale;
iss=(1-(1-delta)*exp(-gammastar-gammamiu))*kss*exp(gammastar+gammamiu);
F=FLss*Lsscale;
yss=yLss*Lsscale;
css=yss/gss-iss;

ssvec  =[css/yss;iss/yss;kss/yss;rss100;100*Rkss]; 
if  flag_repssnames==1;
    ssnames={'c/y'  ;'i/y'  ;'k/y';  'rss100';'Rkss100'};
end
    
%%==========================================================================

%==========================================================================
%% *PART 5. Declare System Matrices* 
% Model to be solved is written as 
% $$ A_{0}Y_{t+1} + A_{1}Y_{t}=A_{2}Y_{t-1} + PSI \eta_{t} $$  
A0  = zeros(NY,NY) ;
A1  = zeros(NY,NY) ;
A2  = zeros(NY,NY) ;
PSI = zeros(NY,NX) ;
%==========================================================================
expg=exp(gammastar);

%% *PART 6. Begin Filling Equation Rows* 

%% Eq 1, Output (y) from production function 
A1(y,y)=1;
A1(y,k)=-((yss+F)/yss)*alpha;
A1(y,L)=-((yss+F)/yss)*(1-alpha);

%% Eq. 2, Effective Capital (K) 
A1(k,k)=1;
A1(k,u)=-1;
A1(k,z)=1;
A2(k,kbar)=1;
A1(k,miu)=alpha/(1-alpha)+1;

%% Eq. 3 Hours (L), from cost minimization 
A1(L,Rk)=1;
A1(L,w)=-1;
A1(L,k)=1;
A1(L,L)=-1;

%% Eq. 4 Return to Capital (Rk) from utilization FOC
A1(Rk,Rk)=1;
A1(Rk,u)=-chi;

%% Eq. 5, Real Wages (w) PC (with split of shocks)
slopewages=(1-beta*xiw)*(1-xiw)/((1+beta)*xiw*(1+niu*(1+1/lambdaw)));
A1(w,w)=1;
A0(w,w)= -beta/(1+beta);
A0(w,p)= -beta/(1+beta);
A1(w,x)=slopewages;
A1(w,z)=(1+iotaw*beta-rhoz*beta)/(1+beta);
A1(w,miu) =(alpha/(1-alpha))*((1+iotaw*beta-rhomiu*beta)) *(1/(1+beta)); 
A1(w,p)=(1+iotaw*beta)/(1+beta);
A1(w,wmarkwn)= -1; 

A2(w,p)=iotaw/(1+beta);
A2(w,w)=1/(1+beta);
A2(w,z)=iotaw/(1+beta);
A2(w,miu)=(alpha*iotaw)/( (1+beta)*(1-alpha) ); 

%% Eq. 6, Wage Gap (x), (MRS-Real Wage)
A1(x,x)=1;
A1(x,w)=-1;
A1(x,lambda)=-1;
A1(x,b)=1/((1-rhob)*(expg-h*beta*rhob)*(expg-h)/(expg*h+expg^2+beta*h^2));
A1(x,L)=niu;
A1(x,psi)=1; 

%% Eq 7, Prices (p) PC 
A1(p,p)=1;
A0(p,p)=-beta/(1+iotap*beta);
A2(p,p)=iotap/(1+iotap*beta);
A1(p,s)=-((1-beta*xip)*(1-xip)/((1+iotap*beta)*xip));
A1(p,lambdap)=-1;       

%% Eq 8, Marginal Cost (s)
A1(s,s)=1;
A1(s,Rk)=-alpha;
A1(s,w)=-(1-alpha);

%% Eq 9, Lagrange Multiplier IBC (Lambda) 
A1(lambda,lambda)=1;
A1(lambda,R)=-1;
A0(lambda,lambda)=-1;
A0(lambda,p)=1;
A1(lambda,z)=rhoz;
A1(lambda,miu)=(rhomiu)*alpha/(1-alpha);

%% Eq 10, Consumption (c)
A1(c,lambda)=(expg-h*beta)*(expg-h);
A1(c,b)=-(expg-h*beta*rhob)*(expg-h)/((1-rhob)*(expg-h*beta*rhob)*(expg-h)/(expg*h+expg^2+beta*h^2)); 
A1(c,z)=-(beta*h*expg*rhoz-h*expg);
A1(c,miu)=(-(beta*h*expg*rhomiu-h*expg))*alpha/(1-alpha);
A1(c,c)=(expg^2+beta*h^2);
A0(c,c)=-beta*h*expg;
A2(c,c)=expg*h;

%% Eq 11, Taylor Rule for Nominal Interest Rate (R)
A1(R,R)=1;
% 10.1 Systematic Component 
A2(R,R)=rhoR;
A1(R,dpma)  = -(1-rhoR)*fp; % Annualized Inflation 
A1(R,yobsan)= -(1-rhoR)*fy; % Annualized Observable Output 
% 10.2 Non-Systematic Component 
A1(R,mp)      = -1;         % Contemporaneous Innovation 
A1(R,sigR_0)  = -1;         % Signals 
A1(R,pitarg)  =(1-rhoR)*fp; % Inflation Drift

%% Eq. 12, Utilization (U) from market clearing condition 
A1(u,c)=css/yss;
A1(u,i)=iss/yss;
A1(u,y)=-1/gss;
A1(u,g)=1/gss;
A1(u,u)=kss*Rkss/yss;

%% Eq. 13, Shadow Value of Capital (phi)
A1(phi,phi)=1;
A0(phi,phi)=-beta*exp(-gammastar-gammamiu)*(1-delta);
A1(phi,z)=rhoz;
A1(phi,miu)=(rhomiu)*(alpha/(1-alpha)+1);
A0(phi,lambda)=-(1-beta*exp(-gammastar-gammamiu)*(1-delta));
A0(phi,Rk)=-(1-beta*exp(-gammastar-gammamiu)*(1-delta));

%% Eq. 14, Investment (i, in consumption units) 
expgmiu=exp(gammastar+gammamiu);
A1(i,lambda)=1/(S*expgmiu^2);
A1(i,phi)=-1/(S*expgmiu^2);
A1(i,upsilon)=-1/(S*expgmiu^2);
A1(i,miu)=((1-beta*rhomiu))*(alpha/(1-alpha)+1); %no normalization
A1(i,i)=(1+beta);
A1(i,z)=(1-beta*rhoz);
A0(i,i)=-beta;
A2(i,i)=1;

%% Eq 15, Capital accumulation (kbar)
% Note: Recall, effective capital above is KBAR*UTILIZATION 
A1(kbar,kbar)=1;
A1(kbar,i)=-(1-(1-delta)*exp(-gammastar-gammamiu));
A1(kbar,upsilon)=-(1-(1-delta)*exp(-gammastar-gammamiu));
A2(kbar,kbar)=(1-delta)*exp(-gammastar-gammamiu);
A1(kbar,z)=(1-delta)*exp(-gammastar-gammamiu);
A1(kbar,miu)=((1-delta)*exp(-gammastar-gammamiu))*(alpha/(1-alpha)+1);

%% Eq 16, MP Exogenous Policy Innovation (unexpected) 
A1(mp,mp)=1; A2(mp,mp)=rhomp; PSI(mp,Rs)=1; 

%% Eq 17, Exogenous Growth Rate in Neutral Technoloy 
A1(z,z)=1; A2(z,z)=rhoz; PSI(z,zs)=1;

%% Eq 18, Exogenous Process in G Spending 
A1(g,g)=1; A2(g,g)=rhog; PSI(g,gs)=1;

%% Eq 19, Exogenous Process in Investment Specific Tech or Relative Price C
% to I 
A1(miu,miu)=1; A2(miu,miu)=rhomiu; PSI(miu,mius)=1;

%% Eq 20, Exogenous Process in Price Markup 
A1(lambdap,lambdap)=1; A2(lambdap,lambdap)=rholambdap;PSI(lambdap,lambdaps)=1;

%% Eq 21, Exogenous Process in Labor Disutility (AR1) 
A1(psi,psi)=1; A2(psi,psi)=rhopsi; PSI(psi,psis)=1;

%% Eq 22, Exogenous Process in Intertemporal Preference
A1(b,b)=1; A2(b,b)=rhob; PSI(b,bs)=1;

%% Eq 23, Exogenous Process in MEI 
A1(upsilon,upsilon)=1; A2(upsilon,upsilon)=rhoupsilon; PSI(upsilon,upsilons)=1;

%% Eq 24, Exogenous Process in Inflation Drift 
A1(pitarg,pitarg)=1; A2(pitarg,pitarg)=rhopitarg; PSI(pitarg,shpitarg)=1;

%% Eq 25, Exogenous Process in Wage Markup (white noise) 
A1(wmarkwn,wmarkwn)=1; PSI(wmarkwn,wmarkwnsh)=1; 

%% Eq. 26, Observable GDP Nominal (add P back)
A1(ynomobs,ynomobs) =  1;
A1(ynomobs,gdp)     = -1; 
A1(ynomobs,dagtrend)= -1; 
% Add inflation, since nominal 
A1(ynomobs,p)       = -1; 

A2(ynomobs,gdp)     = -1; 

%% Eq. 27, Observable Consumption Nominal (add P back)
A1(cnomobs,cnomobs) =1;
A1(cnomobs,c)       = -1; 
A1(cnomobs,dagtrend)= -1; 
% Add inflation, since nominal 
A1(cnomobs,p)= -1; 

A2(cnomobs,c)= -1; 

%% Eq. 28 Observable Investment Nominal (add P back)
% In consumption units
A1(inomobs,inomobs)   =1;
A1(inomobs,i)         = -1; 
A1(inomobs,dagtrend)  = -1; 
% Add inflation, since nominal 
A1(inomobs,p)= -1; 

A2(inomobs,i)= -1; 

%% Eq. 29 Observable Hourly Wages  Nominal (add P back)
A1(wnomobs,wnomobs) =1;
A1(wnomobs,w)       = -1; 
A1(wnomobs,dagtrend)= -1; 
% Add inflation, since nominal 
A1(wnomobs,p)= -1; 

A2(wnomobs,w)= -1; 

%% Eq. 30 Observable Real GDP Growth (in C Units)
% Corrected observation equation for utilization 
% $$ yrobs_{t} = gdp_{t}-gdp_{t-1} + agtrend_{t} $$ 
% where AGTREND is the aggregate trend 
A1(yrobs,yrobs)=1; 
A1(yrobs,gdp)= -1;
A1(yrobs,dagtrend)= -1; 
A2(yrobs,gdp)= -1; 

%% Eq. 31 GDP (detrended), Real Detrended Output corrected for Utilization 
A1(gdp,gdp)= -1; 
A1(gdp,y)=1; 
A1(gdp,u)= -kss*Rkss/yss; 

%% Eq. 32 Growth Rate in Investment Deflator 
% $$ \Delta P^{I}_{t} = \Delta P^{C}_{t} - \mu_{t} $$
A1(dpinv,dpinv)= -1; 
A1(dpinv,p)    =  1; 
A1(dpinv,miu)  = -1; 

%% Eq. 33 GDP Deflator 
% $$ P^{GDP}_{t} = \beta_{1} P^{C}_{t} + \beta_{2} P^{I}_{t} $$ 
% 
% Recall there are 2 ways to define the weights 
%
% *First:* $$ {\frac{C}{C+I} , \frac{I}{C+I} } $$ 
% 
% *Second:* $$ {\frac{C}{Y}  , \frac{I}{Y}   } $$
%
% Note that in second case weights will not add up to one, which is not
% desirable, but opens up the door for adding a 3 deflator, i.e. price of G
% and NX. 
bet_gdpdef=bet_gdpdef(:); 
weights=zeros(1,2); 
if flag_wadd1==1
    weights(1)=css/(css+iss);
    weights(2)=iss/(css+iss);
else
    weights(1)=css/yss;
    weights(2)=iss/yss;
end
A1(gdpdef,gdpdef) = -1; 
A1(gdpdef,p)      = weights(1)*bet_gdpdef(1); 
A1(gdpdef,dpinv)  = weights(2)*bet_gdpdef(2); 

%% Eq. 34 Lag 1 Observable Real GDP Growth (in C Units)
A1(yrobs_1,yrobs_1)=1; A2(yrobs_1,yrobs)=1;

%% Eq. 35 Lag 2 Observable Real GDP Growth (in C Units)
A1(yrobs_2,yrobs_2)=1; A2(yrobs_2,yrobs_1)=1;

%% Eq. 36 Annualized Observable Real GDP Growth (in C Units)
% $$ \Delta y^{A}_{t}= \sum_{j=0}^{3}yrobs_{t-j} $$
A1(yobsan,yobsan)= -1;

A1(yobsan,yrobs)  =1; 
A1(yobsan,yrobs_1)=1; 
A1(yobsan,yrobs_2)=1;
A2(yobsan,yrobs_2)= -1;

%% Eq. 37 Lag 1 Inflation (C)
A1(p_1,p_1)=1; A2(p_1,p)=1;

%% Eq. 38 Lag 2 Inflation (C)
A1(p_2,p_2)=1; A2(p_2,p_1)=1;

%% Eq. 39 Annualized Inflation (C) 
% $$ \Delta P^{A,C}_{t}= \frac{ \sum_{j=0}^{3} \Delta P^{C}_{t-j} }{4} $$
A1(dpma,dpma)= -1;
A1(dpma,p)   = 1/4;
A1(dpma,p_1) = 1/4;
A1(dpma,p_2) = 1/4;
A2(dpma,p_2) = -1/4;

%% Eq. 40, Growth Rate in Aggregate Trend 
% $$ \Gamma_{t} = Z_{t} + \frac{\alpha}{1-\alpha} \mu_{t} $$ 
A1(dagtrend,dagtrend)= -1; 
A1(dagtrend,z)=1; 
A1(dagtrend,miu)=alpha/(1-alpha); 

%% Eq. 45-49 Monetary Policy Signals

%% Eq. 45. $$ \zeta^{4}_{t}       = \epsilon^{4}_{t} $$ 
A1(sigR_4,sigR_4)=1;PSI(sigR_4,Rs_lag4)=1; 

%% Eq. 46.  $$ \zeta^{3}_{t} = \zeta^{4}_{t-1} + \epsilon^{3}_{t} $$ 
A1(sigR_3,sigR_3)=1; A2(sigR_3,sigR_4)=1; PSI(sigR_3,Rs_lag3)=1; 

%% Eq. 47.  $$ \zeta^{2}_{t} = \zeta^{3}_{t-1} + \epsilon^{2}_{t} $$ 
A1(sigR_2,sigR_2)=1; A2(sigR_2,sigR_3)=1; PSI(sigR_2,Rs_lag2)=1; 

%% Eq. 48.  $$ \zeta^{1}_{t} = \zeta^{2}_{t-1} + \epsilon^{1}_{t} $$ 
A1(sigR_1,sigR_1)=1; A2(sigR_1,sigR_2)=1; PSI(sigR_1,Rs_lag1)=1; 

%% Eq. 49. $$ \zeta^{0}_{t} = \zeta^{1}_{t-1} $$, which implies that the history of
% policy signals regarding the current quarter is given by 
%
% $$ \zeta^{0}_{t} = \epsilon^{1}_{t-1} +  \epsilon^{2}_{t-2} + \epsilon^{3}_{t-3} +
% \epsilon^{4}_{t-4}   $$ 
% *Note:* The contemporaneous policy innovation appears 
% directly in the definition of the Taylor rule, in case it is AR(1). An
% alternative would be to make the current composite signal +
% contemporaneous innovation persistent. 
A1(sigR_0,sigR_0)=1;A2(sigR_0,sigR_1)=1;  


%% 
pe_vec=pe_vec(:); 
A1(pe_vec,pe_vec)=eye(length(pe_vec)); 
A0(pe_vec,[p;pe_vec(1:end-1)])= -eye(length(pe_vec)); 

A1(expectedPi40,expectedPi40)= -1; 
A1(expectedPi40,pe_vec      )= (expInfLoading/length(pe_vec))*ones(1,length(pe_vec));  


%% *PART 7: Solve Model using AMASOLVE*
[GG,~,impact,~,~,~,~,eu]=amasolve(A0,A1,A2,zeros(NY,1),PSI) ;
% EU =[1;1] if unique and stable RE solution 
if ~isequal(eu,[1;1]); 
    flag.euok=0; 
    ZZ=[]; C=[]; SDX=[];
    return 
end 

flag.euok=1; 
%% *PART 8: Addition of Measurement Equations for Prices*

%% 8.1 Extend model solution by 2*NSERP rows 
% Number of rows to add to the solution 
nadd=nserp*2; 
% Part 1: Position of prices 
posp =(NY+1):(NY+nserp); 
% Part 2: Position of ID errors 
posid=(posp(end)+1):(NY+2*nserp); 
GG=blkdiag(GG,zeros(nadd)); 
% Matrix of impact coefficients 
RR=zeros(NY+nadd,NX+nserp); 
RR(1:NY,1:NX)=impact; 

%% 8.2 Idiosyncratic errors 
% Which evolve as $$ \nu^{i}_{t} = \rho  \nu^{i}_{t-1} + \varsigma^{i}_{t} $$
% as independent AR(1) processes 
GG(posid,posid   )=diag(rhoid); 
RR(posid,NX+1:end)=eye(nserp); 

%% 8.3 General Structure of Observable Price Equation 
% 
% For price $$ P^{i,C}_{t} = \beta^{i} P^{C}_{t} + \nu^{i}_{t} $$ written in terms
% of lagged variables as 
% $$ P^{i,C}_{t} = \beta^{i} G_{P^{C}} S_{t-1} + \beta^{i} R_{P^{C}}
% \eta_{t} + \rho \nu^{i}_{t-1} + \varsigma^{i}_{t} $$ ,
% where $$ \beta^{i} $$ is the series specific factor loading, and, $$ G_{P^{C}}  R_{P^{C}} $$ correspond to the rows of the solution
% matrix for the model consumption price 

%% 8.3.a First row is GDP 
GG(posp(1),1:NY)=GG(gdpdef,1:NY);
RR(posp(1),1:NX)=RR(gdpdef,1:NX); 

%% 8.3.b Remaining rows are multiple indicators for consumption prices 
% Recall Structural part of observation equation $$ \beta^{i} G_{P^{C}} S_{t-1} +
% \beta^{i} R_{P^{C}} \eta_{t}  $$ 
GG(posp(2:end),:)       =repmat( GG(p,:)    ,[nserp-1 1] ); 
RR(posp(2:end),1:NX)    =repmat( RR(p,1:NX) ,[nserp-1 1] );
% Multiply by Factor Loadings  
bet_c=bet_c(:); 
GG(posp(2:end),1:NY)=(repmat(bet_c,[1 NY])).*GG(posp(2:end),1:NY); 
RR(posp(2:end),1:NX)=(repmat(bet_c,[1 NX])).*RR(posp(2:end),1:NX);
%% 8.3.c Add idiosyncratic errors 
GG(posp,posid)   =diag(rhoid); 
RR(posp,NX+1:end)=eye(nserp); 

% Update model dimensions after adding prices 
impact=RR; 
NY=size(GG,1); 
NX=size(RR,2); 

%% *Part 9: Addition of Measurement Equations for Spread*
% 
%% 9.1 Extend model solution by 1 row 
GG    =blkdiag(GG,0);
impact=[impact zeros(NY,1);zeros(1,NX+1)];

%% 9.2 Fill in last row for the spread 
% 
% $$ sp_{t} = \kappa \upsilon_{t} + \varsigma^{sp}_{t} $$ 
posspread=NY+1;
GG(posspread,:)       =kappa*GG(upsilon,:); 
impact(posspread,:)   =kappa*impact(upsilon,:); 
impact(posspread,NX+1)=1; 

% Update model dimensions after adding spread
NY=NY+1;
NX=NX+1;

%% *PART 10: Declare Cholesky of Variance Covariance Matrix* 
% Recall $$ V = SDX'SDX $$ and the order of the shocks is 
% 1. Structural 2. Policy Signals 3. Idiosyncratic Prices 4. Measurement
% Error Spread 
SDX=diag([sdstruct(:);stdsignals(:);sdid(:);sdspreadme]);
if size(SDX,1)~=NX;error('Dimensions of errors and SDX do not match');end

%% *PART 11: Declare ZZ, Matrix of Pointers to Observables*
% Ordering 
% 1) Nominal GDP Growth. 
% 2) Nominal C   Growth.
% 3) Nominal I   Growth.
% 4) Hours. 
% 5) Nominal W   Growth. 
% 6) Feds Fund Rate. 
% 7) Investment Deflator. 
% 8) GDP Deflator. 
% 9 though NSERP-1) Indicators of C Prices. 
% Last is Spread
dimZ=7+nserp+1+1; 
ZZ=zeros(dimZ,NY); 
ZZ(1,ynomobs)=1; 
ZZ(2,cnomobs)=1; 
ZZ(3,inomobs)=1; 
ZZ(4,L)=1; 
ZZ(5,wnomobs)=1; 
ZZ(6,R)=1;
ZZ(7,dpinv)=1;
ZZ(8:dimZ-2,posp)=eye(nserp);
ZZ(dimZ-1,posspread)=1; 
ZZ(dimZ  ,expectedPi40)=1; 

%% *PART 12: Declare C, Matrix of Constants*
C=zeros(NY,1); 
C(ynomobs)=gammastar100+pss100;
C(cnomobs)=gammastar100+pss100;
C(inomobs)=gammastar100+pss100;
C(L)=Lss;
C(wnomobs)=gammastar100+pss100;
C(R)    =pss100+rss100;
C(dpinv)=(pss100-gammamiu100)+cons_idef; 
C(posp )= pss100*ones(nserp,1)+cprices(:); 
C(expectedPi40)=pss100+constantCPI;

if flag_repstanames==0; 
    return 
end 
%________________________________________________________
%% 16.1 STANAMES: Cell with Names of ALL states 
stanames1={'yhat','khat','hours','Return K','what','x','dp','Marginal Cost','lambdahat','chat',...
    'nomr','Utilization ','phihat','ihat','kbar','mp','z','g','miu','lambdap',...
    'psi','b','upsilon ','pitarg','wmarkwn','GDPm nominal','Cm nominal','Im nominal','Wm nominal',...  % Position 29
    'GDPm real','GDPm level','I deflator','GDPm deflator','GDPm real lag1','GDP real lag2','GDP Annualized','Inflation lag1','Inflation lag2','Inflation annualized','Aggregate trend',...
    'Signal0','Signal1','Signal2',...
    'Signal3','Signal4',...
    'ExpInf1','ExpInf2','ExpInf3','ExpInf4','ExpInf5','ExpInf6','ExpInf7','ExpInf8','ExpInf9','ExpInf10',...
    'ExpInf11','ExpInf12','ExpInf13','ExpInf14','ExpInf15','ExpInf16','ExpInf17','ExpInf18','ExpInf19','ExpInf20',...
    'ExpInf21','ExpInf22','ExpInf23','ExpInf24','ExpInf25','ExpInf26','ExpInf27','ExpInf28','ExpInf29','ExpInf30',...
    'ExpInf31','ExpInf32','ExpInf33','ExpInf34','ExpInf35','ExpInf36','ExpInf37','ExpInf38','ExpInf39','ExpInf40','AvgExpInf10y',...
    'GDP deflator'}; 
if nserp==3 
    stanames2={'C deflator','PCE Core','GDP def Id','C def Id','PCE Core Id'}; 
elseif nserp==4 
    stanames2={'C deflator','PCE Core','CPI Core','GDP def Id','C def Id','PCE Core Id','CPI Core Id'}; 
else 
    disp('Nprices > 4 please add names to STANAMES') 
end 
stanames3={'Spread'}; %,'Spread Me','RealY','RealC','RealI','RealW','SumR'}; 
stanames=[stanames1(:);stanames2(:);stanames3(:)]; 

%% 16.2 STSHOCKS: Cell with Names of Shocks in STANAMES 
% *Note:* Technically not true for the policy signals, for which looking
% only at the cummulative signals. Hence isolate policy innovations rather
% than policy states.
stshocks={'mp','z','g','miu','lambdap','psi','b','upsilon ','pitarg','wmarkwn',...
    'Signal1','Signal2','Signal3','Signal4',...
    'GDP def Id','C def Id','PCE Core Id'};
if nserp==4
    stshocks=[stshocks,'CPI Id'];
end
stshocks=[stshocks,'Spread Me'];
stshocks=stshocks(:);

%% 16.3 SHONAMES: Cell with Names of Shock Innovations 
shonames={'MP','Neutral','G+NX','ISTS','Price Markup','Labor Disutility',...
    'Discount','MEI','Inflation Drift','Wage Markup',...
    'MP Signal 1','MP Signal 2','MP Signal 3','MP Signal 4'}; 
shonames=[shonames(:);stshocks(15:end)]; 


%obsnames={}; for ii=1:size(ZZ,1); temp=stanames( find(ZZ(ii,:)~=0 ) ); obsnames=[obsnames;temp(:)]; end;

%% 16.4 Quick check dimensions 
if length(stanames)~=size(GG,1)
    error('Mistmatch STANAMES and NY');
end
if length(stshocks)~=size(SDX,1)
    error('Mistmatch STSHOCKS and NX');
end
if length(shonames)~=size(SDX,1)
    error('Mistmatch SHONAMES and NX');
end

##### SOURCE END #####
--></body></html>